package cn.mhome.merchant.dao.jpa.business;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.mhome.merchant.dao.common.SqlBuffer;
import cn.mhome.merchant.dao.common.po.Business;
import cn.mhome.merchant.dao.spring.data.repository.BaseJpaDaoSupport;
import cn.mhome.merchant.entity.business.param.BusinessParam;
import cn.mhome.merchant.entity.business.result.BusinessResult;
import cn.mhome.merchant.entity.common.result.Pager;
import cn.mhome.merchant.util.StringUtils;

public class BusinessDaoCustomImpl extends BaseJpaDaoSupport<Business,Long> implements BusinessDaoCustom{
	
	private static String baseQuerySql = "select b.business_id,b.business_name,b.remark,b.create_time,b.business_image,b.city,b.telphone,b.state,b.outlet_name,b.outlet_address,b.outlet_phone,b.outlet_level,"
			+ "b.outlet_desc,b.image_url1,b.image_url2,b.image_url3,b.image_url4,b.image_url5,b.outlet_detail,b.visit_count,b.contact_count ";
	
	private static String baseFromSql = " from business b ";
	@Override
	public List<BusinessResult> findForList(BusinessParam businessParam) {
		SqlBuffer sqlBuffer = new SqlBuffer(baseQuerySql).append(baseFromSql).append(" where 1=1 ");
		Map<String,Object> params = new HashMap<String,Object>();
		handlerParam(businessParam,params,sqlBuffer);
		sqlBuffer.orderby("b.outlet_level desc ,b.create_time");
		return (List<BusinessResult>)super.findBySqlForList(sqlBuffer.toString(),params, BusinessResult.class);
	}
	
	@Override
	public Pager<BusinessResult> findForPage(BusinessParam businessParam) {
		SqlBuffer selectSql = new SqlBuffer(baseQuerySql);
		SqlBuffer fromSql = new SqlBuffer(baseFromSql).append(" where 1=1 ");
		Map<String,Object> params = new HashMap<String,Object>();
		handlerParam(businessParam,params,fromSql);
		fromSql.orderby("b.outlet_level desc,b.create_time");
		return (Pager<BusinessResult>)super.findBySqlForPage(selectSql.toString(),fromSql.toString(),params, BusinessResult.class,businessParam.getPage(),businessParam.getPageSize());
	}
	
	private void handlerParam(BusinessParam classParam ,Map<String, Object> params, SqlBuffer sqlBuffer) {
		if(classParam.getBusinessId() > 0){
			sqlBuffer.append(" and b.business_id =:businessId ");
			params.put("businessId", classParam.getBusinessId());
		}
		if(classParam.getClassTypeId() > 0){
			sqlBuffer.append(" and  ct.class_type_id =:classTypeId ");
			params.put("classTypeId", classParam.getClassTypeId());
		}
		if(classParam.getState() > 0){
			sqlBuffer.append(" and  b.state =:state ");
			params.put("state", classParam.getState());
		}
		if(!StringUtils.isBlank(classParam.getCity())){
			sqlBuffer.append(" and b.city =:city ");
			params.put("city", classParam.getCity());
		}
		if(!StringUtils.isBlank(classParam.getBusinessName())){
			sqlBuffer.append(" and (b.business_name like:businessName OR b.city like:cityKey OR b.outlet_desc like:outletDesc) ");
			params.put("businessName","%"+classParam.getBusinessName()+"%");
			params.put("cityKey","%"+classParam.getBusinessName()+"%");
			params.put("outletDesc","%"+classParam.getBusinessName()+"%");
		}
		
	}
}
